package com.yiwyn.Behavioral.command;

import java.util.Scanner;

public class CommandDemo {

    public static void main(String[] args) {
        // 命令接收者
        BedRoomLight bedRoomLight = new BedRoomLight();
        // 命令
        LightOnCommand lightOnCommand = new LightOnCommand(bedRoomLight);
        LightOffCommand lightOffCommand = new LightOffCommand(bedRoomLight);


        LightController controller = new LightController(lightOnCommand, lightOffCommand);

        Scanner console = new Scanner(System.in);
        while (true) {
            String command = console.next();
            switch (command) {
                case "on":
                    controller.pressLightOn();
                    break;
                case "off":
                    controller.pressLightOff();
                    break;
                case "undo":
                    controller.pressUndo();
                    break;
                case "exit":
                    return;
            }
            System.out.println("灯的状态:" + bedRoomLight.open);
        }
    }


    /**
     * 灯接口
     */
    public abstract static class Light {
        // 开灯
        abstract void on();

        // 关灯
        abstract void off();

        // 状态
        Boolean open;
    }


    /**
     * 卧室灯
     */
    public static class BedRoomLight extends Light {

        @Override
        public void on() {
            System.out.println("BedRoom on");
            this.open = true;
        }

        @Override
        public void off() {
            System.out.println("BedRoom off");
            this.open = false;
        }
    }


    /**
     * 定义“请求” - 命令
     */
    public interface Command {
        // 执行
        void execute();

        // 撤销
        void undo();
    }

    /**
     * 定义“请求” - 开灯命令
     */
    public static class LightOnCommand implements Command {

        private final Light light;

        public LightOnCommand(Light light) {
            this.light = light;
        }

        @Override
        public void execute() {
            light.on();
        }

        @Override
        public void undo() {
            light.off();
        }
    }

    /**
     * 定义“请求” - 关灯命令
     */
    public static class LightOffCommand implements Command {
        private final Light light;

        public LightOffCommand(Light light) {
            this.light = light;
        }

        @Override
        public void execute() {
            light.off();
        }

        @Override
        public void undo() {
            light.on();
        }
    }

    /**
     * 遥控器 有三个按钮：【开】【关】【撤销】
     */
    public static class LightController {

        private final LightOnCommand lightOnCommand;
        private final LightOffCommand lightOffCommand;

        private Command command;

        public LightController(LightOnCommand lightOnCommand, LightOffCommand lightOffCommand) {
            this.lightOnCommand = lightOnCommand;
            this.lightOffCommand = lightOffCommand;
        }

        // 开灯
        public void pressLightOn() {
            this.lightOnCommand.execute();
            command = lightOnCommand;
        }

        // 关灯
        public void pressLightOff() {
            this.lightOffCommand.execute();
            command = lightOffCommand;
        }

        // 撤销
        public void pressUndo() {
            this.command.undo();
        }
    }


}
